#include "CWindow.h"
using namespace GUI;
using namespace Util;

CWindow::CWindow(const int w, const int h) :CGUIObject(w,h)
{
  pTexture = 0;
  pSprite = 0;

  keyR = 0;
  keyG = 0;
  keyB = 0;
  //pGUIObj = new CGUIObject(w,h);
  init();

}

CWindow::~CWindow()
{
  
}


void CWindow::init()
{
  pD3DDevice = Engine::CDevice::getInstance()->getD3DDevice();
  if (FAILED(D3DXCreateSprite(pD3DDevice,&pSprite)))
  {
		LOG_ERROR( "Sikertelen sprite letrehozasi kiserlet." )
    pSprite = 0;
    return;
  }

}
void CWindow::setBackgroundImage(const std::string texture_in, const int r, const int g, const int b)
{
  keyR = r;
  keyG = g;
  keyB = b;
  if (pTexture)
  {
    clean();
    pD3DDevice = Engine::CDevice::getInstance()->getD3DDevice();
  }


 if (texture_in.empty())
  {
		LOG_ERROR( "Nincs megadva textura file nev a sprite letrehozasahoz." )
  }

	if(!fileExists(".\\Textures\\"+texture_in))
	{
		LOG_ERROR( "Nem letezik a megadott mesh file: Textures\\"<< texture_in )
		return;
	}
  textureFile=texture_in;

  D3DXIMAGE_INFO d3dxImageInfo;

	if (FAILED(D3DXCreateTextureFromFileEx( pD3DDevice,
                                 (".\\Textures\\"+textureFile).c_str(),
                                 width, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
                                 height, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE.
                                 1,   // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation.
                                 D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(r,g,b,1.0f),
                                 &d3dxImageInfo,
                                 NULL,
                                 &pTexture )))
	{
		char errorStr[256];
		sprintf_s( errorStr,"Sprite letrehozasi hiba: %s!", textureFile );
		LOG_ERROR( errorStr );
    pTexture = 0;
	}

}
void CWindow::clean()
{
  pD3DDevice = 0;
  if (pTexture)
  {
    pTexture->Release();
    pTexture=0;
  }
  if (pSprite)
  {
    pSprite->Release();
    pSprite = 0;
  }
}

void CWindow::render()
{
  if (visible && pSprite)
  {
    pSprite->Begin( D3DXSPRITE_ALPHABLEND );
    D3DXVECTOR3 vPos = D3DXVECTOR3(posX,posY,0);
    pSprite->Draw( pTexture,
                          NULL,
                          NULL,
                          &vPos,
                          D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,occupacity) );

	  pSprite->End();
  }
}

void CWindow::onLostDevice()
{
  clean();
}

void CWindow::onResetDevice()
{
  init();
  setBackgroundImage(textureFile,keyR,keyG,keyB);
}
/*
void CWindow::setPosition(const float x, const float y)
{
  pGUIObj->setPosition(x,y);
}

void CWindow::setVisible(const bool v)
{
  pGUIObj->setVisible(v);
}

void CWindow::setOccupacity(const float o)
{
  pGUIObj->setOccupacity(o);
}

bool CWindow::isVisible()
{
  return pGUIObj->isVisible();
}

float CWindow::getPosX()
{
  return pGUIObj->getPosX();
}

float CWindow::getPosY()
{
  return pGUIObj->getPosY();
}

float CWindow::getOccupacity()
{
  return pGUIObj->getOccupacity();
}

int CWindow::getWidth()
{
  return pGUIObj->getWidth();
}

int CWindow::getHeight()
{
  return pGUIObj->getHeight();

}

*/